<?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\DB;
use Laravel\Passport\HasApiTokens;
use Illuminate\Foundation\Auth\User as Authenticatable;
use phpDocumentor\Reflection\Types\Self_;
use Spatie\Permission\Traits\HasRoles;
use App\Notifications\ResetPasswordNotification;

class Chapter extends Authenticatable
{

    use HasApiTokens,Notifiable;


    protected $table = 'chapter';

    public    $timestamps = false;
    protected $primaryKey = 'id';


    /**
     * 插入章节
     * @param array $data
     * @return bool
     */
    public function create(array $data):bool {
        $order_id=self::where('cid',$data['cid'])->max('order');
        if($order_id){
            $new_order_id=$order_id+1;
        }else{
            $new_order_id=1;
        }
        $res['cid']=$data['cid'];
        $res['title']=$data['title'];
        $res['order']=$new_order_id;

        if(self::insert($res)){
            return true;
        }else{
            return false;
        }
    }


    /**
     * 章节交换位置
     * @param array $data
     * @return bool
     */
    public function sort(array $data):bool{
        $exchange_order=self::where('id',$data['exchange_id'])->value('order');
        $order=self::where('id',$data['id'])->value('order');

        if(empty($exchange_order) || empty($order)) return false;
        DB::transaction(function () use($data,$exchange_order,$order){
            self::where('id',$data['exchange_id'])->update(['order' => $order]);

            self::where('id',$data['id'])->update(['order' => $exchange_order]);
        });
        return true;
    }

    /**
     * 章节删除
     * @param int $id
     * @return bool
     */
    public function del(int $id):bool {
        DB::transaction(function () use($id){
            self::where('id',$id)->delete();
            DB::table('chapter_picture')->where('cid',$id)->delete();
        });
        return true;
    }

    /**
     * 项目章节列表
     * @param array $data
     * @return array
     */
    public function list(array $data):array{
        $res=self::where('cid',$data['cid'])->select(['id','order','title'])
            ->forPage($data['page'], $data['pageSize'])
            ->orderBy('order','asc')
            ->get()
            ->toArray();

        if($res){
            foreach ($res as $key=>&$val){
                $val['subtitle']='第'.numToWord($key+1).'章';
            }
        }
        $count=self::where('cid',$data['cid'])->count();
        $result['list']=$res;
        $result['total']=$count;
        return $result;
    }


    /**
     * 保存章节图片
     * @param array $data
     * @return bool
     */
    public function savePic(array $data):bool {
        if($data['location_id']){
            $order_id=DB::table('chapter_picture')->where('id',$data['location_id'])->value('order');
            $order=$order_id+$data['location'];
        }else{
            //初始化第一张
            $order=1;
        }

        $res['cid']=$data['id'];
        $res['pic_uri']=$data['pic_uri'];
        $res['order']=$order;
        $res['matter_list']=$data['matter_list'];
        $res['second_pic_uri']=$data['second_pic_uri'];
        $res['second_matter_list']=$data['second_matter_list'];
        if(isset($data['canvas_width'])){
            $res['width']=$data['canvas_width'];
        }
        if(isset($data['canvas_height'])){
            $res['height']=$data['canvas_height'];
        }
        DB::transaction(function () use($data,$res){
            if($data['location']==1){
                //下一张
                DB::table('chapter_picture')->where([['cid','=',$data['id']],['order','>=',$res['order']]])->increment('order');
            }else{
                //上一张
                DB::table('chapter_picture')->where([['cid','=',$data['id']],['order','<=',$res['order']]])->decrement('order');
            }
            DB::table('chapter_picture')->insert($res);
        });

        return true;
    }

    public function picInfo(array $data):array{
        $res=DB::table('chapter_picture')->where('id',$data['pid'])->select(['pic_uri','matter_list','cid','order','second_pic_uri','second_matter_list'])->get()->toArray();
        return $res;
    }

    public function picEdit(array $data):bool {
        $up['pic_uri']=$data['pic_uri'];
        $up['matter_list']=$data['matter_list'];
        $res=DB::table('chapter_picture')->where('id',$data['pid'])->update($up);
        return $res;
    }
    /**
     * 章节图片列表
     * @param array $data
     * @return array
     */
    public function picList(array $data):array {
        $res=DB::table('chapter_picture')
            ->select(['id','pic_uri','created_at','order','width','height','second_pic_uri'])
            ->where('cid',$data['cid'])->orderBy('order','asc')
            ->forPage($data['page'], $data['pageSize'])
            ->get()
            ->toArray();
        $count=DB::table('chapter_picture')->where('cid',$data['cid'])->count();
        $result['list']=$res;
        $result['total']=$count;
        return $result;
    }

    public function picDel(int $id):bool {
        $bool=DB::table('chapter_picture')->delete($id);
        return $bool;
    }

}
